<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:c="http://java.sun.com/jsp/jstl/core"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:p="http://primefaces.prime.com.tr/ui"
                template="./../template/WorkspaceTemplate.xhtml">

    <ui:define name="top">

    </ui:define>

    <ui:define name="left">

    </ui:define>

    <ui:define name="right">

    </ui:define>

    <script type="text/javascript">
        var chartStyle = {
            padding:20,
            legend: {
                display:"right",
                spacing:10
            }
        }
    </script>

    <ui:define name="content">
        <h:form id="financialStatusForm" style="font-size: 85%">
            <h2>View Financial Dashboard</h2>
            <br/>
            <p:panel>

                <h:panelGrid columns="3" cellpadding="10">
                    <h:outputLabel style="font-weight: bold" value="Cash Available (Current): "/>
                    <p:inputText value="#{fdManagerBean.cashOnHand.doubleValue}"/>
                    <h:outputText value=""/>
                    <h:outputLabel style="font-weight: bold" value="Required Safety Cash: "/>
                    <p:inputText value="#{fdManagerBean.safetyCash.doubleValue}"/>
                    <p:commandButton image="ui-icon ui-icon-calculator" actionListener="#{fdManagerBean.refresh}" update="financialStatusForm"/>
                    <h:outputLabel style="font-weight: bold" value="Company Financial Status: "/>
                    <h:outputText value="#{fdManagerBean.status}"/>
                    <h:outputText value=""/>
                    <h:outputText value=""/>
                    <ui:repeat value="#{fdManagerBean.dangerMonths}" var="m">
                        <h:outputText value="#{m}   "/>
                    </ui:repeat>
                </h:panelGrid>                   

                <h:panelGrid columns ="2" style="width: 100%">
                    <p:fieldset legend="Future Cash Position">
                        <p:lineChart value="#{fdManagerBean.cash}" var="rec" xfield="#{rec.month}" width="100%">
                            <p:chartSeries label="Cash Position" value="#{rec.totalCash}" />
                            <p:chartSeries label="Receivables" value="#{rec.receivables}" />
                            <p:chartSeries label="Payables" value="#{rec.payables}" />
                        </p:lineChart>
                    </p:fieldset>
                    <p:graphicImage value="/images/legend.png" width="100px" height="70px" style="text-align: right"/>
                </h:panelGrid>

                <br/>
                <br/>

                <p:tabView>

                    <p:tab title="Transactions">
                        <br/>
                        <p:dataTable var="rec" value="#{fsManagerBean.allTransactions}" paginator="true" rows="10" style="font-size: 80%">  

                            <f:facet name="header">  
                                Financial Transactions
                            </f:facet>

                            <p:columnGroup type="header">  
                                <p:row>  
                                    <p:column rowspan="2" headerText="Type"/>
                                    <p:column rowspan="2" headerText="Date"/>
                                    <p:column rowspan="2" headerText="Customer / Supplier"/>
                                    <p:column colspan="2" headerText="Cash" />
                                    <p:column colspan="2" headerText="Receivable" />
                                    <p:column colspan="2" headerText="Payable" />
                                </p:row>

                                <p:row>  
                                    <p:column headerText="Debit" />  
                                    <p:column headerText="Credit" />
                                    <p:column headerText="Debit" />  
                                    <p:column headerText="Credit" />  
                                    <p:column headerText="Debit" />  
                                    <p:column headerText="Credit" />  
                                </p:row>
                            </p:columnGroup>

                            <p:column >  
                                #{rec.transactionType}  
                            </p:column>

                            <p:column >
                                <h:outputText value="#{rec.date}">
                                    <f:convertDateTime pattern="dd-MMM-yy hh:mm"/>
                                </h:outputText>
                            </p:column>

                            <p:column >
                                #{rec.companyName}  
                            </p:column>

                            <p:column>
                                <h:outputText value="#{rec.cashDebit}">
                                    <f:convertNumber minFractionDigits="2"/>
                                </h:outputText>
                            </p:column>

                            <p:column>
                                <h:outputText value="#{rec.cashCredit}">
                                    <f:convertNumber minFractionDigits="2"/>
                                </h:outputText>
                            </p:column>

                            <p:column>
                                <h:outputText value="#{rec.receivableDebit}">
                                    <f:convertNumber minFractionDigits="2"/>
                                </h:outputText>
                            </p:column>

                            <p:column>
                                <h:outputText value="#{rec.receivableCredit}">
                                    <f:convertNumber minFractionDigits="2"/>
                                </h:outputText>
                            </p:column>

                            <p:column>
                                <h:outputText value="#{rec.payableDebit}">
                                    <f:convertNumber minFractionDigits="2"/>
                                </h:outputText>
                            </p:column>

                            <p:column>
                                <h:outputText value="#{rec.payableCredit}">
                                    <f:convertNumber minFractionDigits="2"/>
                                </h:outputText>
                            </p:column>

                            <p:columnGroup type="footer">  
                                <p:row>  
                                    <p:column colspan="3" footerText="Total:"  
                                              style="text-align:right"  />  
                                    <p:column colspan="2" footerText="$ #{fsManagerBean.cashTotal}" />  
                                    <p:column colspan="2" footerText="$ #{fsManagerBean.receivableTotal}" /> 
                                    <p:column colspan="2" footerText="$ #{fsManagerBean.payableTotal}" />  
                                </p:row>  
                            </p:columnGroup> 

                        </p:dataTable>  
                    </p:tab>

                    <p:tab title="Outstanding Customer Receivables">
                        <br/>
                        <p:dataTable var="rec" value="#{fsManagerBean.allUnpaidInvoices}" paginator="true" rows="10">  

                            <f:facet name="header">  
                                Customer Receivable
                            </f:facet>

                            <p:column sortBy="#{rec.customer.clientId}" filterBy="#{rec.customer.clientId}">  
                                <f:facet name="header">  
                                    <h:outputText value="Client ID" />  
                                </f:facet>  
                                <h:outputText value="#{rec.customer.clientId}" />  
                            </p:column>

                            <p:column sortBy="#{rec.customer.companyName}" filterBy="#{rec.customer.companyName}">  
                                <f:facet name="header">  
                                    <h:outputText value="Company Name" />  
                                </f:facet>  
                                <h:outputText value="#{rec.customer.companyName}" />  
                            </p:column>

                            <p:column sortBy="#{rec.deliveryOrder.salesOrder.salesOrderIdStr}" filterBy="#{rec.deliveryOrder.salesOrder.salesOrderIdStr}">  
                                <f:facet name="header">  
                                    <h:outputText value="Sales Order #" />  
                                </f:facet>  
                                <h:outputText value="#{rec.deliveryOrder.salesOrder.salesOrderIdStr}" />  
                            </p:column>

                            <p:column sortBy="#{rec.invoiceId}" filterBy="#{rec.invoiceId}">  
                                <f:facet name="header">  
                                    <h:outputText value="Invoice #" />  
                                </f:facet>  
                                <h:outputText value="#{rec.invoiceId}" />  
                            </p:column>

                            <p:column sortBy="#{rec.total}" filterBy="#{rec.total}">  
                                <f:facet name="header">  
                                    <h:outputText value="Amount Due" />  
                                </f:facet>  
                                <h:outputText value="#{rec.total}">
                                    <f:convertNumber type="currency"/>
                                </h:outputText>  
                            </p:column>  

                        </p:dataTable>  
                    </p:tab>

                    <p:tab title="Outstanding Supplier Payables">
                        <br/>
                        <p:dataTable var="rec" value="#{fsManagerBean.allUnpaidSupplierInvoices}" paginator="true" rows="10">  

                            <f:facet name="header">  
                                Supplier Payable
                            </f:facet>

                            <p:column sortBy="#{rec.supplierInvoiceNo}" filterBy="#{rec.supplierInvoiceNo}">  
                                <f:facet name="header">  
                                    <h:outputText value="Supplier Invoice #" />  
                                </f:facet>  
                                <h:outputText value="#{rec.supplierInvoiceNo}" />  
                            </p:column>

                            <p:column sortBy="#{rec.invoiceAmount}" filterBy="#{rec.invoiceAmount}">  
                                <f:facet name="header">  
                                    <h:outputText value="Amount Due" />  
                                </f:facet>
                                <h:outputText value="#{rec.invoiceAmount}">
                                    <f:convertNumber type="currency"/>
                                </h:outputText>  
                            </p:column>

                        </p:dataTable>  
                    </p:tab>

                </p:tabView>
                <br/>
                <br/>
                <br/>
            </p:panel>
            <br/>
            <br/>
            <br/>
        </h:form>

    </ui:define>

    <ui:define name="bottom">

    </ui:define>

</ui:composition>
